package org.example.statistics.service.impl;

import java.util.Calendar;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.example.contract.domain.Contract;
import org.example.contract.mapper.ContractMapper;
import org.example.utils.UserType;
import org.example.utils.Util;
import org.example.warehouse.domain.MallStoreDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.statistics.mapper.StatisticsBuyMapper;
import org.example.statistics.domain.StatisticsBuy;
import org.example.statistics.service.IStatisticsBuyService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 统计采购报Service业务层处理
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-05-31
 */
@Service
public class StatisticsBuyServiceImpl implements IStatisticsBuyService
{
    @Autowired
    private StatisticsBuyMapper statisticsBuyMapper;

    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询统计采购报
     *
     * @param statisticsBuyId 统计采购报主键
     * @return 统计采购报
     */
    @Override
    public StatisticsBuy selectStatisticsBuyByStatisticsBuyId(Long statisticsBuyId)
    {
        return statisticsBuyMapper.selectStatisticsBuyByStatisticsBuyId(statisticsBuyId);
    }

    /**
     * 查询统计采购报列表
     *
     * @param statisticsBuy 统计采购报
     * @return 统计采购报
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<StatisticsBuy> selectStatisticsBuyList(StatisticsBuy statisticsBuy)
    {
        return statisticsBuyMapper.queryStatisticsBuyByGroupByMonth(statisticsBuy);
       // return statisticsBuyMapper.selectStatisticsBuyList(statisticsBuy);
    }

    /**
     * 新增统计采购报
     *
     * @param statisticsBuy 统计采购报
     * @return 结果
     */
    @Override
    public int insertStatisticsBuy(StatisticsBuy statisticsBuy)
    {
        statisticsBuy.setCreateTime(DateUtils.getNowDate());
        return statisticsBuyMapper.insertStatisticsBuy(statisticsBuy);
    }

    /**
     * 修改统计采购报
     *
     * @param statisticsBuy 统计采购报
     * @return 结果
     */
    @Override
    public int updateStatisticsBuy(StatisticsBuy statisticsBuy)
    {
        return statisticsBuyMapper.updateStatisticsBuy(statisticsBuy);
    }

    /**
     * 批量删除统计采购报
     *
     * @param statisticsBuyIds 需要删除的统计采购报主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsBuyByStatisticsBuyIds(String statisticsBuyIds)
    {
        return statisticsBuyMapper.deleteStatisticsBuyByStatisticsBuyIds(Convert.toStrArray(statisticsBuyIds));
    }

    /**
     * 删除统计采购报信息
     *
     * @param statisticsBuyId 统计采购报主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsBuyByStatisticsBuyId(Long statisticsBuyId)
    {
        return statisticsBuyMapper.deleteStatisticsBuyByStatisticsBuyId(statisticsBuyId);
    }

    @Override
    @Transactional
    public int insertStatisticsBuyByMonth(StatisticsBuy statisticsBuy) {

        int count=0;
        Contract contract= new Contract();
        contract.setCreateTime(statisticsBuy.getCreateTime());
        contract.setContractType(Util.CONTRACT_TYPE_BUY);
        List<Contract> contracts = contractMapper.selectStatisticsContractList(contract);
        statisticsBuyMapper.batchInsert(contracts);
        count++;


        //统计入库单
        MallStoreDetail mallStoreDetail_1 =new MallStoreDetail();
        mallStoreDetail_1.setCreateTime(statisticsBuy.getCreateTime());
        List<StatisticsBuy> list1 = statisticsBuyMapper.selectStatisticsBuyMallStoreTotalMoneyByMonth(mallStoreDetail_1);
        if(list1 !=null &&  list1.size()>0){
            statisticsBuyMapper.batchUpdate(list1);
            count++;
        }

        //统计对账单
        List<StatisticsBuy> list2 = statisticsBuyMapper.selectStatisticsBuyAcountTotalMoneyByMonth(mallStoreDetail_1);
        if(list2 !=null && list2.size()>0){
            statisticsBuyMapper.batchUpdate(list2);
            count++;
        }


        //统计支付
        statisticsBuy.setCreateTime(statisticsBuy.getCreateTime());
        List<StatisticsBuy> list3 = statisticsBuyMapper.selectStatisticsPayMoneyByMonth(statisticsBuy);
        if(list3 !=null && list3.size()>0){
            statisticsBuyMapper.batchUpdate(list3);
            count++;
        }

        return count;
    }

    @Override
    @DataScope(deptAlias = "d")
    public List<StatisticsBuy> queryStatisticsBuyByGroupByMonth(StatisticsBuy statisticsBuy) {
        if(Util.isGongYingShangRole()){
            statisticsBuy.setRemark(UserType.GONGYINGSHANG.getValue());
            statisticsBuy.getParams().put("deptId", ShiroUtils.getSysUser().getDeptId());
        }
        return statisticsBuyMapper.queryStatisticsBuyByGroupByMonth(statisticsBuy);
    }

    @Override
    @DataScope(deptAlias = "d")
    public StatisticsBuy selectStatisticsBuyByCurrentMonth(StatisticsBuy statisticsBuy){
        if(Util.isGongYingShangRole()){
            statisticsBuy.setRemark("1");
        }else{
            statisticsBuy.setRemark("0");
        }
        statisticsBuy.setCreateTime(DateUtils.getNowDate());
        return statisticsBuyMapper.selectStatisticsBuyByCurrentMonth(statisticsBuy);
    }
}
